package com.gimi.cloud.jimi.merchantZone.consumer.service.ueditor.imp;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.merchantZone.consumer.feign.UeditorFeignService;
import com.gimi.cloud.jimi.merchantZone.consumer.service.ueditor.UeditorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangwei on 2018/2/1.
 */
@Service
public class UeditorServiceImpl implements UeditorService {
    private static Logger logger = LoggerFactory.getLogger(UeditorServiceImpl.class);
    private final UeditorFeignService ueditorFeignService;

    @Autowired
    public UeditorServiceImpl(UeditorFeignService ueditorFeignService){
        this.ueditorFeignService = ueditorFeignService;
    }


    /**
     * editor操作
     */
    @Override
    public Response ueditorAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultiValueMap multipartFiles = new LinkedMultiValueMap();
        try {
            if(request instanceof StandardMultipartHttpServletRequest) {
                StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
                multipartFiles = multipartHttpServletRequest.getMultiFileMap();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //请求参数
        Map<String,String> paramsData = new HashMap<>();
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()){
            String name = names.nextElement();
            paramsData.put(name,request.getParameter(name));
        }

        String params = JSON.toJSONString(paramsData);
        return ueditorFeignService.ueditorAction(paramsData,multipartFiles);
    }



}
